#include "widget.h"
#include "ui_widget.h"
#include<QDebug>
#include<QHostAddress>
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    m_pSocket=NULL;
}

Widget::~Widget()
{
    delete ui;
}

void Widget::InitClientWidget()
{
    qDebug()<<"Widget::InitClientWidget start";
    if(NULL!=m_pSocket)
    {
        return;
    }
    m_pSocket=new QTcpSocket(this);
    connect(m_pSocket,SIGNAL(readyRead()),this,SLOT(OnReadyRead()));
    qDebug()<<"Widget::InitClientWidget end";
}

void Widget::OnReadyRead()
{
    qDebug()<<"Widget::OnReadyRead start";
    QByteArray arr=m_pSocket->readAll();
    qDebug()<<arr;
    ui->receivedata->setText(QString(arr));
    qDebug()<<"Widget::OnReadyRead end";
}



void Widget::on_connectbt_clicked()
{
    qDebug()<<"Widget::on_connectbt_clicked start";
    if(NULL==m_pSocket)
    {
        return;
    }
    m_pSocket->connectToHost(QHostAddress("127.0.0.1"),8888);
    if(m_pSocket->waitForConnected(1000))
    {
        qDebug()<<"connect ok";
    }
    qDebug()<<"Widget::on_connectbt_clicked end";
}

void Widget::on_sendbt_clicked()
{
    qDebug()<<"Widget::on_sendbt_clicked start";
    QString str=ui->lineEdit->text();
    if(str.length()<=0)
    {
        return;
    }
    m_pSocket->write(str.toStdString().data());
    ui->receivedata->setText(str.toStdString().data());
    qDebug()<<"Widget::on_sendbt_clicked end";
}
